package com.ohuang.test_compose.test

import android.annotation.SuppressLint
import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Button
import androidx.compose.material.Text
import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.Snapshot
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.ohuang.test_compose.util.StackMsg
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

private const val TAG = "TestPage"
private var mysate= mutableStateOf("")

var aa=derivedStateOf { mysate.value.length }
@SuppressLint("UnrememberedMutableState")
@Composable
fun TestPage() {
    Log.d(TAG, "TestPage: ")
    Column(modifier = Modifier.padding(10.dp)) {
        Test0()

        val rememberCoroutineScope = rememberCoroutineScope()

        Button(onClick = {
            Snapshot.registerApplyObserver { anies: Set<Any>, snapshot: Snapshot ->
                Log.d(TAG, "registerApplyObserver: anies=$anies ,snapshot=$snapshot")
            }
            Snapshot.registerGlobalWriteObserver {
                Log.d(TAG, "registerGlobalWriteObserver: any=${it} ")
            }
        }) {
            Text(text = "注册监听")
        }

        Button(onClick = {
            rememberCoroutineScope.launch(Dispatchers.IO) {


                delay(1000)
                mysate.value = "123"
                mutableStateOf.value = "abc"
                val takeMutableSnapshot = Snapshot.takeMutableSnapshot()
                takeMutableSnapshot.enter { testSnapshot() }
                takeMutableSnapshot.apply()

            }
            Log.d(TAG, "mutableStateOf= ${mutableStateOf.value}")
        }) {
            Text(text = "测试")
        }

        Button(onClick = {
            Log.d(TAG, "mutableStateOf= ${mutableStateOf.value}")
            Log.d(TAG, "aa= ${aa.value}")
        }) {
            Text(text = "查看值")
        }
    }

}

@Composable
private fun Test0() {
    Log.d(TAG, "Test0: ")
    val text = getText()
    MyText(text)
}

@Composable
private fun MyText(text: String) {
    Log.d(TAG, "MyText: ")
    Text(text = text)
}

@Composable
fun getText(): String {

    Log.d(TAG, "getText: " + StackMsg.getStackMsg())
    val aa = remember() {
        mutableStateOf("")
    }


    LaunchedEffect(key1 = Unit, block = {

        delay(1000)
        aa.value = "123"


    })
    Snapshot.takeMutableSnapshot(readObserver = {
        Log.d(TAG, "getText:readObserver  $it")
    }, writeObserver = {
        Log.d(TAG, "getText:writeObserver  $it")
    })
    Log.d(TAG, "getText: ")

    return aa.value
}

private val mutableStateOf = mutableStateOf("", policy = object : SnapshotMutationPolicy<String> {
    override fun equivalent(a: String, b: String): Boolean {
        Log.d(TAG, "equivalent: $a  $b")
        return a == b
    }

    override fun merge(previous: String, current: String, applied: String): String? {
        Log.d(TAG, "merge: previous= $previous  current=$current applied=$applied")
        return applied
    }

})

fun testSnapshot() {


    mutableStateOf.value = "123"
    Log.d(TAG, "testSnapshot: v0 " + mutableStateOf.value)
    val takeMutableSnapshot = Snapshot.takeMutableSnapshot(readObserver = {
        Log.d(TAG, "testSnapshot:readObserver  $it")
    }, writeObserver = {
        Log.d(TAG, "testSnapshot:writeObserver  $it")
    })


    takeMutableSnapshot.enter {
        Log.d(TAG, "testSnapshot: v1 " + mutableStateOf.value)
        mutableStateOf.value = "456"
        Log.d(TAG, "testSnapshot:v2  " + mutableStateOf.value)
    }

    takeMutableSnapshot.apply()
    Log.d(TAG, "testSnapshot:v3  " + mutableStateOf.value)

    Log.d(TAG, "testSnapshot:v4 " + mutableStateOf.value)
}






